package ua.org.nuos.sdms.clientgui.client.model;

import ua.org.nuos.sdms.clientgui.client.events.EntityChangedEvent;
import ua.org.nuos.sdms.clientgui.client.listeners.EntityChangedListener;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: dio
 * Date: 22.03.12
 * Time: 22:55
 * To change this template use File | Settings | File Templates.
 */
public class EntityContainer<T> {
    
    private T entity;
    private List<EntityChangedListener<T>> listeners;

    public EntityContainer() {
        this(null);
    }

    public EntityContainer(T entity) {
        this.entity = entity;
        listeners = new LinkedList<EntityChangedListener<T>>();
    }

    public T getEntity() {
        return entity;
    }

    public void setEntity(T entity) {
        this.entity = entity;
        fireListeners();
    }

    public void addListener(EntityChangedListener<T> listener) {
        listeners.add(listener);
    }

    public void fireListeners() {
        EntityChangedEvent<T> event = new EntityChangedEvent<T>(this);
        for(EntityChangedListener<T> listener : listeners) {
            listener.changed(event);
        }
    }

    public void clear() {
        setEntity(null);
    }
}
